package cn.tedu.boot32.conreoller;

import cn.tedu.boot32.entity.User;
import cn.tedu.boot32.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserController {
    @Autowired
    UserMapper mapper;

    @RequestMapping("/reg")
    public int reg(@RequestBody User user){
        //拿用户输入的用户名去数据库中查询用户信息
        User u = mapper.selectByUsername(user.getUsername());
        if(u!=null){
            return 2;
        }
        mapper.insert(user);
        return 1;
    }

    @RequestMapping("/login")
    public int login(@RequestBody User user){
        //那用户输入的用户名查询数据库里面的数据信息
        User u = mapper.selectByUsername(user.getUsername());
        if(u!=null){//说明用户名存在
            if(user.getPassword().equals(u.getPassword())){//说明密码正确
                return 1;//登陆成功
            }
                return 3;//密码错误
        }
        return 2;//用户名不存在
    }
}
